﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class frmQuanHe : Form
    {
        public frmQuanHe()
        {
            InitializeComponent();
        }
        int MaNK;
        int MaSHK;
        int MaSHKCu;

        public frmQuanHe(int MaNK, int MaSHK,int MaSHKCu)
        {
            InitializeComponent();
            this.MaNK = MaNK;
            this.MaSHK = MaSHK;
            this.MaSHKCu = MaSHKCu;
        }

        private int _Success;

        public int Success
        {
            get { return _Success; }
            set { _Success = value; }
        }
        
        private void frmQuanHe_Load(object sender, EventArgs e)
        {
            cmbQuanHe.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHe.DisplayMember = "TenQuanHe";
            cmbQuanHe.ValueMember = "MaQH";

            cmbNguoiCap.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";

            dateNgayCap.Value = DateTime.Now;

            cmbCanBoDangKi.Text = frmDangNhap.user_login.HoTen;

            txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV).TenChucVu;
            txtChucVu.Enabled = false;

            txtNoiChuyen.Text = MaSHK.ToString();
            txtSoChuyenKhau.Text = DungChung.SoChuyenKhau().ToString();
        }

        private void btnXong_Click(object sender, EventArgs e)
        {
            List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(MaSHKCu);
            if (txtLyDoChuyen.Text == "")
            {
                MessageBox.Show("Vui lòng nhập lý do", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtLyDoChuyen.Focus();
                return;
            }
            Success = 1;
            NhanKhauBLL.UpdateQuanHe(MaNK,(byte)cmbQuanHe.SelectedValue);
            NhanKhauBLL.UpdateMaSHK(MaSHK, MaNK);

            ChuyenKhauPublic ck = new ChuyenKhauPublic();
            ck.MaNK = MaNK;
            ck.MaCK = DungChung.LoadMaCK();
            ck.SoChuyenKhau = int.Parse(txtSoChuyenKhau.Text);
            ck.NoiChuyen = int.Parse(txtNoiChuyen.Text);
            ck.LyDoChuyen = txtLyDoChuyen.Text;
            ck.NgayNhan = dateNgayCap.Value;
            ck.NgayCap = dateNgayCap.Value;
            ck.CanBoNoiCap = cmbCanBoDangKi.Text;
            ck.CanBoNoiNhan = cmbCanBoDangKi.Text;
            ck.ThuongTru = MaSHKCu.ToString();
            ck.MaQH = (byte)cmbQuanHe.SelectedValue;
            ck.ChuHo = list_nk.Find(item=>item.MaQH==1).HoTen;
            ck.NguoiCap = cmbNguoiCap.Text;
            ck.NguoiNhan = cmbNguoiCap.Text;
            ck.ChucVuNguoiCap =NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV;
            ck.ChucVuNguoiNhan = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV;
            ck.NoiCap =ThietLapBLL.LoadMaDiaChi();
            ChuyenKhauBLL.InsertChuyenKhau(ck);
            //frmChuyenDi cd = new frmChuyenDi();
            //cd.Refresh();
            this.Close();
            MessageBox.Show("Chuyển khẩu thành công");

        }

        private void cmbNguoiCap_SelectionChangeCommitted(object sender, EventArgs e)
        {
            txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV).TenChucVu;
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            Success = -1;
            this.Close();
        }

        private void txtLyDoChuyen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtLyDoChuyen.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

       
    }
}
